package com.google.android.vending.licensing;

import android.content.Context;
import android.util.Log;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;

public class APKExpansionPolicy
  implements Policy
{
  private static final String DEFAULT_MAX_RETRIES = "0";
  private static final String DEFAULT_RETRY_COUNT = "0";
  private static final String DEFAULT_RETRY_UNTIL = "0";
  private static final String DEFAULT_VALIDITY_TIMESTAMP = "0";
  public static final int MAIN_FILE_URL_INDEX = 0;
  private static final long MILLIS_PER_MINUTE = 60000L;
  public static final int PATCH_FILE_URL_INDEX = 1;
  private static final String PREFS_FILE = "com.android.vending.licensing.APKExpansionPolicy";
  private static final String PREF_LAST_RESPONSE = "lastResponse";
  private static final String PREF_MAX_RETRIES = "maxRetries";
  private static final String PREF_RETRY_COUNT = "retryCount";
  private static final String PREF_RETRY_UNTIL = "retryUntil";
  private static final String PREF_VALIDITY_TIMESTAMP = "validityTimestamp";
  private static final String TAG = "APKExpansionPolicy";
  private Vector<String> mExpansionFileNames = new Vector();
  private Vector<Long> mExpansionFileSizes = new Vector();
  private Vector<String> mExpansionURLs = new Vector();
  private int mLastResponse;
  private long mLastResponseTime = 0L;
  private long mMaxRetries;
  private PreferenceObfuscator mPreferences;
  private long mRetryCount;
  private long mRetryUntil;
  private long mValidityTimestamp;

  public APKExpansionPolicy(Context paramContext, Obfuscator paramObfuscator)
  {
    this.mPreferences = new PreferenceObfuscator(paramContext.getSharedPreferences("com.android.vending.licensing.APKExpansionPolicy", 0), paramObfuscator);
    this.mLastResponse = Integer.parseInt(this.mPreferences.getString("lastResponse", Integer.toString(291)));
    this.mValidityTimestamp = Long.parseLong(this.mPreferences.getString("validityTimestamp", "0"));
    this.mRetryUntil = Long.parseLong(this.mPreferences.getString("retryUntil", "0"));
    this.mMaxRetries = Long.parseLong(this.mPreferences.getString("maxRetries", "0"));
    this.mRetryCount = Long.parseLong(this.mPreferences.getString("retryCount", "0"));
  }

  private Map<String, String> decodeExtras(String paramString)
  {
    HashMap localHashMap = new HashMap();
    try
    {
      Iterator localIterator = URLEncodedUtils.parse(new URI("?" + paramString), "UTF-8").iterator();
      while (localIterator.hasNext())
      {
        NameValuePair localNameValuePair = (NameValuePair)localIterator.next();
        String str = localNameValuePair.getName();
        int i = 0;
        while (localHashMap.containsKey(str))
        {
          StringBuilder localStringBuilder = new StringBuilder().append(localNameValuePair.getName());
          i++;
          str = i;
        }
        localHashMap.put(str, localNameValuePair.getValue());
      }
    }
    catch (URISyntaxException localURISyntaxException)
    {
      Log.w("APKExpansionPolicy", "Invalid syntax error while decoding extras data from server.");
    }
    return localHashMap;
  }

  private void setLastResponse(int paramInt)
  {
    this.mLastResponseTime = System.currentTimeMillis();
    this.mLastResponse = paramInt;
    this.mPreferences.putString("lastResponse", Integer.toString(paramInt));
  }

  private void setMaxRetries(String paramString)
  {
    try
    {
      Long localLong2 = Long.valueOf(Long.parseLong(paramString));
      localLong1 = localLong2;
      this.mMaxRetries = localLong1.longValue();
      this.mPreferences.putString("maxRetries", paramString);
      return;
    }
    catch (NumberFormatException localNumberFormatException)
    {
      while (true)
      {
        Log.w("APKExpansionPolicy", "Licence retry count (GR) missing, grace period disabled");
        paramString = "0";
        Long localLong1 = Long.valueOf(0L);
      }
    }
  }

  private void setRetryCount(long paramLong)
  {
    this.mRetryCount = paramLong;
    this.mPreferences.putString("retryCount", Long.toString(paramLong));
  }

  private void setRetryUntil(String paramString)
  {
    try
    {
      Long localLong2 = Long.valueOf(Long.parseLong(paramString));
      localLong1 = localLong2;
      this.mRetryUntil = localLong1.longValue();
      this.mPreferences.putString("retryUntil", paramString);
      return;
    }
    catch (NumberFormatException localNumberFormatException)
    {
      while (true)
      {
        Log.w("APKExpansionPolicy", "License retry timestamp (GT) missing, grace period disabled");
        paramString = "0";
        Long localLong1 = Long.valueOf(0L);
      }
    }
  }

  private void setValidityTimestamp(String paramString)
  {
    try
    {
      Long localLong2 = Long.valueOf(Long.parseLong(paramString));
      localLong1 = localLong2;
      this.mValidityTimestamp = localLong1.longValue();
      this.mPreferences.putString("validityTimestamp", paramString);
      return;
    }
    catch (NumberFormatException localNumberFormatException)
    {
      while (true)
      {
        Log.w("APKExpansionPolicy", "License validity timestamp (VT) missing, caching for a minute");
        Long localLong1 = Long.valueOf(60000L + System.currentTimeMillis());
        paramString = Long.toString(localLong1.longValue());
      }
    }
  }

  public boolean allowAccess()
  {
    long l = System.currentTimeMillis();
    boolean bool1;
    if (this.mLastResponse == 256)
    {
      boolean bool4 = l < this.mValidityTimestamp;
      bool1 = false;
      if (!bool4)
        bool1 = true;
    }
    boolean bool3;
    do
    {
      boolean bool2;
      do
      {
        int i;
        do
        {
          return bool1;
          i = this.mLastResponse;
          bool1 = false;
        }
        while (i != 291);
        bool2 = l < 60000L + this.mLastResponseTime;
        bool1 = false;
      }
      while (!bool2);
      if (l <= this.mRetryUntil)
        break;
      bool3 = this.mRetryCount < this.mMaxRetries;
      bool1 = false;
    }
    while (bool3);
    return true;
  }

  public String getExpansionFileName(int paramInt)
  {
    if (paramInt < this.mExpansionFileNames.size())
      return (String)this.mExpansionFileNames.elementAt(paramInt);
    return null;
  }

  public long getExpansionFileSize(int paramInt)
  {
    if (paramInt < this.mExpansionFileSizes.size())
      return ((Long)this.mExpansionFileSizes.elementAt(paramInt)).longValue();
    return -1L;
  }

  public String getExpansionURL(int paramInt)
  {
    if (paramInt < this.mExpansionURLs.size())
      return (String)this.mExpansionURLs.elementAt(paramInt);
    return null;
  }

  public int getExpansionURLCount()
  {
    return this.mExpansionURLs.size();
  }

  public long getMaxRetries()
  {
    return this.mMaxRetries;
  }

  public long getRetryCount()
  {
    return this.mRetryCount;
  }

  public long getRetryUntil()
  {
    return this.mRetryUntil;
  }

  public long getValidityTimestamp()
  {
    return this.mValidityTimestamp;
  }

  public void processServerResponse(int paramInt, ResponseData paramResponseData)
  {
    Map localMap;
    Iterator localIterator;
    if (paramInt != 291)
    {
      setRetryCount(0L);
      if (paramInt == 256)
      {
        localMap = decodeExtras(paramResponseData.extra);
        this.mLastResponse = paramInt;
        setValidityTimestamp(Long.toString(60000L + System.currentTimeMillis()));
        localIterator = localMap.keySet().iterator();
      }
    }
    else
    {
      while (true)
      {
        if (!localIterator.hasNext())
          break label345;
        String str = (String)localIterator.next();
        if (str.equals("VT"))
        {
          setValidityTimestamp((String)localMap.get(str));
          continue;
          setRetryCount(1L + this.mRetryCount);
          break;
        }
        if (str.equals("GT"))
          setRetryUntil((String)localMap.get(str));
        else if (str.equals("GR"))
          setMaxRetries((String)localMap.get(str));
        else if (str.startsWith("FILE_URL"))
          setExpansionURL(-1 + Integer.parseInt(str.substring("FILE_URL".length())), (String)localMap.get(str));
        else if (str.startsWith("FILE_NAME"))
          setExpansionFileName(-1 + Integer.parseInt(str.substring("FILE_NAME".length())), (String)localMap.get(str));
        else if (str.startsWith("FILE_SIZE"))
          setExpansionFileSize(-1 + Integer.parseInt(str.substring("FILE_SIZE".length())), Long.parseLong((String)localMap.get(str)));
      }
    }
    if (paramInt == 561)
    {
      setValidityTimestamp("0");
      setRetryUntil("0");
      setMaxRetries("0");
    }
    label345: setLastResponse(paramInt);
    this.mPreferences.commit();
  }

  public void resetPolicy()
  {
    this.mPreferences.putString("lastResponse", Integer.toString(291));
    setRetryUntil("0");
    setMaxRetries("0");
    setRetryCount(Long.parseLong("0"));
    setValidityTimestamp("0");
    this.mPreferences.commit();
  }

  public void setExpansionFileName(int paramInt, String paramString)
  {
    if (paramInt >= this.mExpansionFileNames.size())
      this.mExpansionFileNames.setSize(paramInt + 1);
    this.mExpansionFileNames.set(paramInt, paramString);
  }

  public void setExpansionFileSize(int paramInt, long paramLong)
  {
    if (paramInt >= this.mExpansionFileSizes.size())
      this.mExpansionFileSizes.setSize(paramInt + 1);
    this.mExpansionFileSizes.set(paramInt, Long.valueOf(paramLong));
  }

  public void setExpansionURL(int paramInt, String paramString)
  {
    if (paramInt >= this.mExpansionURLs.size())
      this.mExpansionURLs.setSize(paramInt + 1);
    this.mExpansionURLs.set(paramInt, paramString);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.google.android.vending.licensing.APKExpansionPolicy
 * JD-Core Version:    0.6.2
 */